package com.yanqu.road.server.gameplayer.module.useract;

import com.yanqu.road.dao.impl.player.UserActDataDaoImpl;
import com.yanqu.road.entity.player.UserActData;
import com.yanqu.road.pb.player.PlayerProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.protocol.ClientProtocol;

import java.util.Map;

public class UserActModule extends GeneralModule {

    private UserActData userActData;

    public UserActModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        this.userActData = new UserActDataDaoImpl().getUserActData(player.getUserId());
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return true;
    }

    @Override
    public boolean saveData() {
        if (this.userActData != null) {
            if (this.userActData.isInsertOption()) {
                new UserActDataDaoImpl().add(this.userActData);
            } else if (this.userActData.isUpdateOption()) {
                new UserActDataDaoImpl().update(this.userActData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        this.syncUserActData();
    }

    /**
     * 同步数据
     */
    public void syncUserActData() {
        if (this.userActData != null) {
            PlayerProto.PlayerActDataMsg.Builder builder = PlayerProto.PlayerActDataMsg.newBuilder();
            //已完成引导的活动类型
            builder.addAllComGuideActType(this.userActData.getComGuideActTypes());
            //2选1正确选项数据
            for (Map.Entry<String, Map<String, String>> entry : this.userActData.getChooseTrueIndexMap().entrySet()) {
                String actType = entry.getKey();
                Map<String, String> trueChooseIndexMap = entry.getValue();
                PlayerProto.PlayerActChooseData.Builder actChooseBuilder = PlayerProto.PlayerActChooseData.newBuilder();
                actChooseBuilder.setActType(actType);
                for (Map.Entry<String, String> trueEntry : trueChooseIndexMap.entrySet()) {
                    String chooseId = trueEntry.getKey();
                    String trueChooseIndex = trueEntry.getValue();
                    PlayerProto.PlayerActChooseTrueData.Builder actChooseItemBuilder = PlayerProto.PlayerActChooseTrueData.newBuilder();
                    actChooseItemBuilder.setChooseId(chooseId);
                    actChooseItemBuilder.setChooseTureIndex(trueChooseIndex);
                    actChooseBuilder.addTrueDataList(actChooseItemBuilder);
                }
                builder.addActChooseDataList(actChooseBuilder);
            }
            player.sendPacket(ClientProtocol.U_SYNC_USER_ACT_DATA_MSG, builder);
        }
    }

    /**
     * 初始化数据
     * @return
     */
    public synchronized void initData() {
        if (this.userActData == null) {
            this.userActData = new UserActData(player.getUserId());
        }
    }

    /**
     * 获取key
     * @param activityType
     * @param activityChildType
     * @return
     */
    private String getKey(int activityType, int activityChildType) {
        return activityType + "_" + activityChildType;
    }

    /**
     * 添加已完成引导的活动类型
     * @param activityType
     */
    public synchronized void addCompleteGuideActivityType(int activityType, int activityChildType) {
        if (this.userActData == null) {
            this.initData();
        }
        String key = this.getKey(activityType, activityChildType);
        if (!this.userActData.getComGuideActTypes().contains(key)) {
            this.userActData.addComGuideActType(key);
            this.userActData.setUpdateTime(System.currentTimeMillis());
            //同步一下
            this.syncUserActData();
        }
    }

    /**
     * 判断活动是否已经完成引导
     * @param activityType
     * @return
     */
    public boolean isCompleteGuideActivity(int activityType, int activityChildType) {
        if (this.userActData == null) {
            this.initData();
        }
        String key = this.getKey(activityType, activityChildType);
        return this.userActData.getComGuideActTypes().contains(key);
    }

    /**
     * 记录2选1正确选项
     * @param activityType
     */
    public synchronized void addActTrueChooseIndex(int activityType, int activityChildType, String chooseId, String trueChooseIndex) {
        if (this.userActData == null) {
            this.initData();
        }
        //判断是否已经记录过
        String key = this.getKey(activityType, activityChildType);
        String oldTrueChooseIndex = this.userActData.getChooseTrueIndex(key, chooseId);
        if (oldTrueChooseIndex == null) {
            this.userActData.addChooseTrueIndex(key, chooseId, trueChooseIndex);
            //同步一下
            this.syncUserActData();
        }
    }
}
